/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmLogin.java
 *
 * Created on 22 Sep 12, 12:49:13
 */
package sistemperpustakaan.view;

import com.conn.SettingUI;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.ImageIcon;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import sistemperpustakaan.controller.UserController;
import sistemperpustakaan.model.UserModel;

/**
 *
 * @author EKo
 */
public class FrmLogin extends javax.swing.JDialog {

    UserController controller;
    UserModel model;
    private Connection jConnection;
    private SettingUI jConfig;
    private static Connection mConnect;

    /** Creates new form FrmLogin */
    public FrmLogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
//        ImageIcon icon = new ImageIcon(getClass().getResource("../image/ico.png"));
//        this.setIconImage(icon.getImage());
        setTitle("Sistem Informasi Perpustakaan");
        setSize(400, 300);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (dim.width - getWidth()) / 2;
        int y = (dim.height - getHeight()) / 2;
        setLocation(x, y);
        setButton();



        initializeLookAndFeel();
        cekKoneksi();
        controller = new UserController();
        model = new UserModel();
        controller.setFrameLogin(this);
        controller.setModel(model);
        txtUsername.requestFocus();

    }

    public static void initializeLookAndFeel() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JPasswordField getTxtPassword() {
        return txtPassword;
    }

    public void setTxtPassword(JPasswordField txtPassword) {
        this.txtPassword = txtPassword;
    }

    public JTextField getTxtUsername() {
        return txtUsername;
    }

    public void setTxtUsername(JTextField txtUsername) {
        this.txtUsername = txtUsername;
    }

    public SettingUI getjConfig() {
        return jConfig;
    }

    public void setjConfig(SettingUI jConfig) {
        this.jConfig = jConfig;
    }

    public Connection getjConnection() {
        return jConnection;
    }

    public void setjConnection(Connection jConnection) {
        this.jConnection = jConnection;
    }

    public void setButton() {
        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnLogin.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                login();
            }
        });
    }

    private void login() {
        if (controller.login()) {
            controller.selectUser(txtUsername.getText(), txtPassword.getText());

            dispose();
            FrmMain main = new FrmMain();
            main.setVisible(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        bg = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        bg1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login Sistem Perpustakaan");
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(400, 300));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 204, 204));
        jLabel1.setText("Username");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 100, -1, -1));

        txtUsername.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txtUsername.setText("a");
        txtUsername.setBorder(null);
        txtUsername.setOpaque(false);
        txtUsername.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtUsernameKeyPressed(evt);
            }
        });
        getContentPane().add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 110, 170, 20));

        bg.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        bg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/bg_username.png"))); // NOI18N
        getContentPane().add(bg, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 100, 200, 40));

        txtPassword.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txtPassword.setText("a");
        txtPassword.setBorder(null);
        txtPassword.setOpaque(false);
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });
        getContentPane().add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(173, 156, 170, -1));

        bg1.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        bg1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/bg_username.png"))); // NOI18N
        getContentPane().add(bg1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 150, 200, 30));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        jLabel5.setText("Masukkan usernama dan Password");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(204, 204, 204));
        jLabel2.setText("Password");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 150, -1, -1));

        btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/btn_login.png"))); // NOI18N
        btnLogin.setBorder(null);
        btnLogin.setBorderPainted(false);
        btnLogin.setContentAreaFilled(false);
        btnLogin.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/btn_login.png"))); // NOI18N
        btnLogin.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/btn_login_over.png"))); // NOI18N
        getContentPane().add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 190, 80, 30));

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/btn_cancel.png"))); // NOI18N
        btnCancel.setBorder(null);
        btnCancel.setBorderPainted(false);
        btnCancel.setContentAreaFilled(false);
        btnCancel.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/btn_cancel.png"))); // NOI18N
        btnCancel.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/btn_cancel_over.png"))); // NOI18N
        getContentPane().add(btnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 190, 90, 30));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/bg_splash.png"))); // NOI18N
        jLabel3.setPreferredSize(new java.awt.Dimension(400, 300));
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtUsernameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUsernameKeyPressed
        if (evt.getKeyCode() == 10) {
            txtPassword.requestFocus();
        }
    }//GEN-LAST:event_txtUsernameKeyPressed

    private void txtPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        if (evt.getKeyCode() == 10) {
            login();
        }
    }//GEN-LAST:event_txtPasswordKeyPressed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bg;
    private javax.swing.JLabel bg1;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    private void cekKoneksi() {
        initializeLookAndFeel();
        // TODO code application logic here
        jConfig = new SettingUI(null, true);
        jConfig.makeConnect();
        if (jConfig.isConnected() == true) {
            mConnect = jConfig.getConnection();
        } else {
            jConfig.setVisible(true);
            mConnect = jConfig.getConnection();
            if (jConfig.isConnected() == false) {
                System.exit(1);
            }
        }
    }
}
